package org.cloud.encdec.controller;

import cn.hutool.crypto.SecureUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import org.cloud.core.redis.RedisUtil;
import org.cloud.dimension.annotation.SystemResource;
import org.cloud.utils.RsaUtil;
import org.cloud.vo.CommonApiResult;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@Api(value = "UserEncController", tags = "用户加密管理")
@Lazy
@RestController
@RequestMapping("/common/enc")
@SystemResource(path = "/common/enc")
public class UserEncController {

    @Value("${system.rsa.base-key:system:rsa:}")
    private String eccKey;


    /**
     * 加密,不需要权限
     *
     * @return 加密后的字符
     * @throws Exception 异常
     */
    @ApiOperation(value = "getPublicKey", notes = "获取临时公钥")
    @RequestMapping(value = "/getPublicKey", method = RequestMethod.POST)
    public CommonApiResult<String> encrypt() throws Exception {
        List<String> eccKeys = RsaUtil.single().getRsaKeys();
        RedisUtil.single().set(SecureUtil.md5(eccKeys.get(0)), eccKeys.get(1), 5L);
        return CommonApiResult.createSuccessResult(eccKeys.get(0));
    }

}
